package com.atguigu.business;

import com.atguigu.business.utils.Constant;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.get.GetResult;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * Created by wuyufei on 08/08/2017.
 */
@SpringBootApplication
public class BizApplication {

    public static void main(String[] args) {
        SpringApplication.run(BizApplication.class, args);
    }
    public static void main1(String[] args) throws IOException, ExecutionException, InterruptedException {

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
        RestHighLevelClient esClient = context.getBean(RestHighLevelClient.class);
        GetRequest getRequest = new GetRequest(Constant.ES_INDEX,"3062");
        GetResponse getResponse = esClient.get(getRequest, RequestOptions.DEFAULT);

        //Map<String,GetField> filed = getResponse.getFields();

        Object value = getResponse.getSourceAsMap().get("tags");

        if(value == null){
            UpdateRequest updateRequest = new UpdateRequest(Constant.ES_INDEX,Constant.ES_MOVIE_TYPE,"3062");
            updateRequest.doc(XContentFactory.jsonBuilder().startObject()
            .field("tags","abc")
            .endObject());
            GetResult getResult = esClient.update(updateRequest, RequestOptions.DEFAULT).getGetResult();
            System.out.println(getResult.getSource());
        }else{
            UpdateRequest updateRequest = new UpdateRequest(Constant.ES_INDEX,"2542");
            updateRequest.doc(XContentFactory.jsonBuilder().startObject()
                    .field("tags",value+"|abc")
            .endObject());
            GetResult getResult = esClient.update(updateRequest, RequestOptions.DEFAULT).getGetResult();
            System.out.println(getResult);
        }

        System.out.println(Math.round(4.466D));

    }

}
